package com.ruoyi.kenuo.controller;


import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.kenuo.domain.KernelVerison;
import com.ruoyi.kenuo.service.IKernelVerisonService;
import com.ruoyi.system.service.ISysUserEquService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Api(tags="微信相关接口")
@RestController
@RequestMapping("/kenuo/wx")
public class WxController extends BaseController {
 /*   @Autowired
    private IKernelUserEquService kernelUserEquService;*/
    @Autowired
    private ISysUserEquService iSysUserEquService;

    @Autowired
    private IKernelVerisonService kernelVerisonService;
    @Value("${ruoyi.httpLu}")
    private String httpLu;

    private final String appId= "wx7ef052d6b6051995";
    private final String appSecret= "9092fe17b9de1c54ce523c491fbe061c";


    @ApiOperation(value = "获取微信token",notes = "获取微信token")
    @GetMapping("/wxOpen/getMytkoOne")
    public AjaxResult getMytko() {
        //获取access_token
        String acurl = "https://api.weixin.qq.com/cgi-bin/token";
        HttpUtils ht = new HttpUtils();
        String str1 =  ht.sendGet(acurl,"grant_type=client_credential&appid="+appId+"&secret="+appSecret);
        // 转成Json对象 获取openid
        JSONObject jsonObject = JSONObject.parseObject(str1);
        return AjaxResult.success(jsonObject);
    }
    /**
     * 查询最新版本
     */
    @ApiOperation(value = "查询当前最新版本信息",notes = "查询当前最新版本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "versionNum", value = "版本号，不传就是查询目前最新的版本", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "remarkOne", value = "1:台式版本  2:头戴版本", required = false, dataType = "String")
    })
    @GetMapping("/wxOpen/getNewlist")
    public AjaxResult getNewlist(KernelVerison kernelVerison)
    {
        List<KernelVerison> list = kernelVerisonService.selectKernelVerisonList(kernelVerison);
        if(list.size()>0){
            KernelVerison kev = list.get(0);
            return AjaxResult.success(kev);
        }else {
            return AjaxResult.error("暂无数据");
        }
    }
    /**
     * 查询设备是否被绑定
     *
     */
/*    @ApiOperation(value="查询设备和用户之间是否被绑定", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "snCode", value = "设备sn码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "remarkOne", value = "设备手机号", required = true, dataType = "String")
    })
    @Log(title = "查询设备是否被绑定", businessType = BusinessType.INSERT)
    @PostMapping("/wxOpen/selectEquUser")
    public AjaxResult selectEquUser(@RequestBody KernelUserEqu kernelUserEqu){
        String snCode = kernelUserEqu.getSnCode();
        String tel = kernelUserEqu.getRemarkOne();//手机号
        if(StringUtils.isEmpty(snCode)){
            return AjaxResult.error("参数snCode未传");
        }
        if(StringUtils.isEmpty(tel)){
            return AjaxResult.error("参数remarkOne未传");
        }
        //查询设备是否被绑定
        if(StringUtils.isNotEmpty(snCode)){
            MyKernelUserEqu userEqu = new MyKernelUserEqu();
            userEqu.setSnCode(snCode);
            //查询设备是否被绑定
            List<KernelUserEqu> equList = kernelUserEquService.selectCurUserEqu(kernelUserEqu);
            List<Map<String,Object>> elist = iSysUserEquService.selectKernelEquList(userEqu);
            if(equList.size()>0){
                return AjaxResult.error("请使用该设备绑定账号进行登录");
            }
            if(elist.size()<=0){
                return AjaxResult.error("当前设备不存在");
            }
        }
        //查询设备是否被绑定
        List<KernelUserEqu> list = kernelUserEquService.selectKernelUserEquList(kernelUserEqu);
        if(list.size()>0){
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error("请先绑定此设备");
        }

    }*/

    /**
     * 获取微信openId
     *
     */
    @ApiOperation(value="获取微信openId", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "code", value = "微信的code", required = true, dataType = "String")
    })
    @GetMapping("/wxOpen/getOpenId")
    public AjaxResult getOpenId(String code)
    {
        Map<String,Object> bm = new HashMap<>();
        if(StringUtils.isNotEmpty(code)){
            bm.put("code",code);
        }
        //微信的接口
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appId+
                "&secret="+appSecret+"&js_code="+ code +"&grant_type=authorization_code";
        //获取access_token
        HttpUtils ht = new HttpUtils();
        String str = ht.sendGet(url,"");
        // 转成Json对象 获取openid
        JSONObject jsonObject = JSONObject.parseObject(str);
        return AjaxResult.success(jsonObject);
    }

    /**
     * 前端上传图片

    @Log(title = "上传图片")
    @PostMapping("/upPic")
    public AjaxResult upPic(@RequestParam("file") MultipartFile file) throws Exception
    {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);

        return AjaxResult.success("操作成功",ipUrl+fileName);
    }
     */
    /**
     * 前端上传文件
     */
    @Log(title = "上传文件")
    @PostMapping("/upWenjian")
    public AjaxResult upWenjian(@RequestParam("file") MultipartFile file) throws Exception
    {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        long size = file.getSize();
        Map<String,Object> mp = new LinkedHashMap<>();
        mp.put("size",size);
        mp.put("fileName",httpLu+fileName);
        return AjaxResult.success(mp);
    }
}
